import{User} from '../models/User';
import{Token} from '../models/Token';
import{AuthenticateUser} from '../models/AuthenticateUser';
import{BaseService} from './BaseService';
import{UsersDao} from '../dao/UsersDao';
import{TokensDao} from '../dao/TokensDao';
export class UsersService extends BaseService
{
  usersDao:UsersDao;
  tokensDao:TokensDao;
  constructor()
  {
    super();
    this.usersDao = new UsersDao();
    this.tokensDao = new TokensDao();
  }

  login(username:string,password:string,callback:Function){
    this.usersDao.get(username,password,(err1,user:User)=>{
      if(!err1 && user)
      {
        this.tokensDao.add(user._id,(err2,token:Token)=>{
          var authUser:AuthenticateUser = new AuthenticateUser();
          authUser.user = user;
          authUser.token = token;
          callback(err2,authUser);
        })
      }else{
        callback(err1);
      }
    });
  }

  register(user:User,callback:Function){
    this.usersDao.add(user,callback);
  }

  update(user:User,callback:Function)
  {
    this.usersDao.update(user,callback);
  }
}
